package IceAssembly;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * This class controls the flow of the program.<p>
 * <u>Member Variables:</u><br>
 *  code_string : private static String[]<br>
 *  location_counter : public static int<br>
 * Change Log:
 *  April 14,2008 - Initial code entry - Aashish Kumar.<br>
 *  April 22,2008 - no longer extends convet.java - added by mistake - Aashish Kumar.<br>
 *  April 22,2008 - checkLineErrors() member function no longer exits - Aashish Kumar.<br>
 * @author Yogesh Jindal
 */
public class Control implements Interface {
    
    private static String[] code_string = new String[0];
    public static int location_counter;
    public static int line_num;
    public static String debug_status = "OFF";//default value
    public static Convert convert_instance = new Convert();
    public static Tokenize tokenize_instance = new Tokenize();
    public static Process process_instance = new Process();
    public static Initialize initialize_instance = new Initialize();
    public static Control control_instance = new Control();
    public static Table table_instance = new Table();
    public static String code_file = "src//TestPrograms//default.txt";
    public static String name_of_test = "default";
    
    /**
     * This function initializes code_string[] from the code file
     * whose URL String is declared in the Interface.java.<p>
     * Change Log:<br>
     *  4/16/08 - Initial Code Entry - Aashish Kumar<p>
     * Errors produced:<br>
     * None<p>
     * Date of module being installed: 4/16/08
     * @author Aashish Kumar
     * @return Returns 1 if successful else returns 0.
    **/
    private int getCodeFile () {
        try{
            File file = new File(code_file); System.out.println(code_file);
            BufferedReader buffer_reader = new BufferedReader(new FileReader(file));
            int no_lines=0;
            while(buffer_reader.readLine()!=null){
               no_lines++;
            }
            code_string = new String[no_lines]; 
            buffer_reader = new BufferedReader(new FileReader(file));
            int index=0; String temp;
            while((temp=buffer_reader.readLine())!=null){
                code_string[index]= temp; index++;
            }
        }catch(IOException e){
            System.out.println("file reading exception from Control.java.getcodefile(); function");
            return 0;
        }
        return 1;
    }
    
    /**
     * This function reads the lines in the code_string[],
     * it then remove the comments and optimize the spaces
     * and then tokenizes the string and calls the process
     * instruction function...<p>
     * Change Log:<br>
     *  4/16/08 - Initial Code Entry - Aashish Kumar<p>
     * Errors produced:<br>
     * None<p>
     * Date of module being installed: 4/16/08
     * @author Aashish Kumar
    **/
    private void readLine () {        
        for(line_num=0; line_num<code_string.length; line_num++){
            String temp = code_string[line_num];            
            temp = convert_instance.getNonCommentString(temp);
            temp = convert_instance.optimizeTheSpaces(temp);
            if(temp.compareTo("")==0 || temp.compareTo(" ")==0){
                if(line_num==0){//a blank line or a comment line
                    table_instance.addToErrorTable("001");//fatal error
                    return;
                }
            }else{
                String[] tokened_string= tokenize_instance.tokenize(temp);
                int ret_value = process_instance.processInstruction(tokened_string);
                if(ret_value==1000){
                    table_instance.addToErrorTable("003");
                    return;
                }else if(ret_value==2000){
                    table_instance.addToErrorTable("004");
                    return;
                }else if(ret_value==50){
                    //everything was fine or errors were handled
                }else if(ret_value==5){ 
                    line_num++;
                    while(line_num<code_string.length){
                        table_instance.addToErrorTable("210");
                        line_num++;
                    }
                }else if(ret_value==4000){
                    return;
                }
            }    
        }
    }
        
    /**
     * The main will read the file whose url is in the Interface.java.
     * Command line file reading still needs to be done.<p>
     * Change Log:<br>
     *  4/16/08 - Initial Code Entry - Aashish Kumar<p>
     * Errors produced:<br>
     * None<p>
     * Date of module being installed: 4/16/08
     * @author Aashish Kumar
     * @param args takes the input file from command line(may be)...
     */
    public static void main(String[] args){        
        control_instance.getCodeFile();
        control_instance.readLine();
        table_instance.addToSymbolFile();
        table_instance.addToInterFile();
        table_instance.addToErrorFile();
    }
}

